<?php
/**
 * @author guoxiaoyong
 */
namespace bc\model\wx;

use bc\ar\MActiveRecord;
use bc\wx\JsApi;

class JsApiModel extends MActiveRecord
{
	
	public static function tableName()
	{
		return '{{%wx_jsapi}}';
	}
	
	
	
	/**
	 * 获取ticket
	 * @return string $ticket
	 */
	public function getTicket($accessToken)
	{
		$ticket = '';
		$expiresTime = time() - 7200;
		$params = 'create_time >= '.$expiresTime;
		
		$row = $this->getRow($params);
		
		if(!$row)
		{
			$api = new JsApi();
			$rs = $api->getJsApi($accessToken);
			if($rs['errcode'] == 0)
			{
				$data['ticket'] = $rs['ticket'];
				$data['create_time'] = time();
				$this->add($data);
				
				$ticket = $rs['ticket'];
			}
		}
		else 
		{
			$ticket = $row['ticket'];
		}
		
		return $ticket;
	}
	
}

